package site.shihuan.helpdesk.auth.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.shihuan.helpdesk.auth.dao.AuthTenantDao;
import site.shihuan.helpdesk.auth.dao.AuthUserDao;
import site.shihuan.helpdesk.auth.dao.TenantJoinRequestDao;
import site.shihuan.helpdesk.auth.model.bo.UpdateUserBO;
import site.shihuan.helpdesk.auth.model.po.AuthTenantPO;
import site.shihuan.helpdesk.auth.model.po.AuthUserPO;
import site.shihuan.helpdesk.auth.model.po.SvcClerkPO;
import site.shihuan.helpdesk.auth.model.po.TenantJoinRequestPO;
import site.shihuan.helpdesk.auth.model.vo.TenantJoinVO;
import site.shihuan.helpdesk.auth.model.vo.UserVO;
import site.shihuan.helpdesk.common.util.IdWorker;

import java.util.List;

/**
 * 租户业务层
 *
 * @author 周世焕
 * @Date 2021/3/20 16:11
 */
@Service
@Slf4j
public class TenantService {

    @Autowired
    private AuthTenantDao authTenantDao;

    @Autowired
    private AuthUserDao authUserDao;

    @Autowired
    private ClerkService clerkService;

    @Autowired
    private TenantJoinRequestDao joinRequestDao;

    /**
     * 根据site查询
     * @param site
     * @return
     */
    public AuthTenantPO findBySite(String site) {
        return authTenantDao.selectBySite(site);
    }

    /**
     * 查询全部
     * @return
     */
    public List<AuthTenantPO> findAll() {
        return authTenantDao.selectAll();
    }

    /**
     * 根据Id查询
     * @param tenantId
     * @return
     */
    public AuthTenantPO findById(String tenantId) {
        return authTenantDao.selectByPrimaryKey(tenantId);
    }

    /**
     * 保存租户信息
     * @param authTenantPO
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public AuthTenantPO saveTenant(AuthTenantPO authTenantPO) {
        if(authTenantPO.getTenantId() != null) {
            authTenantDao.updateByPrimaryKeySelective(authTenantPO);
        }else {
            authTenantPO.setTenantId(IdWorker.nextId());
            SvcClerkPO clerkPO = new SvcClerkPO();
            clerkPO.setTenantId(authTenantPO.getTenantId());
            clerkPO.setClerkId(authTenantPO.getOwner());
            changeTenant(authTenantPO.getTenantId(),authTenantPO.getOwner());
            authTenantDao.insertSelective(authTenantPO);
            clerkService.createClerk(clerkPO);
        }
        return authTenantPO;
    }

    /**
     * 加入企业 / 邀请成员
     * @param requestPO
     */
    public void joinTenant(TenantJoinRequestPO requestPO) {
        requestPO.setJoinId(IdWorker.nextId());
        joinRequestDao.insertSelective(requestPO);
    }

    /**
     * 查看我的加入企业申请
     * @param userId
     * @return
     */
    public List<TenantJoinVO> findMyJoinRequest(String userId) {
        return joinRequestDao.selectByUserId(userId);
    }

    /**
     * 更改用户所在的租户账号
     * @param tenantId
     * @param userId
     */
    public void changeTenant(String tenantId, String userId) {
        AuthUserPO userPO = new AuthUserPO();
        userPO.setTenantId(tenantId);
        userPO.setUserId(userId);
        authUserDao.updateByPrimaryKeySelective(userPO);
    }

    /**
     * 根据tenantId查询
     * @param tenantId
     * @return
     */
    public List<UserVO> findPeopleList(String tenantId, String deptId,String userName) {
        return authUserDao.selectByTenant(tenantId, deptId, userName);
    }

    public void deletePeopleBatch(String tenantId, String[] userIds) {
        authUserDao.deletePeopleFromTenant(tenantId,userIds);
    }

    /**
     * 租户管理员对用户信息修改
     * @param userBO
     */
    public void updatePeopleByTenant(UpdateUserBO userBO) {
        authUserDao.updatePeopleByTenant(userBO);
    }

    /**
     * 查询企业加入请求
     * @param tenantId
     * @return
     */
    public List<TenantJoinRequestPO> findJoinRequests(String tenantId) {
        return joinRequestDao.selectByTenantId(tenantId);
    }

    /**
     * 同意加入企业
     * @param userIds
     */
    @Transactional(rollbackFor = Exception.class)
    public void joinConfirm(String tenantId,String[] userIds) {
        joinRequestDao.joinReply(tenantId, userIds, 1);
        authUserDao.updateTenantIdBatch(tenantId,userIds);
        for(String userId : userIds) {
            SvcClerkPO clerkPO = new SvcClerkPO();
            clerkPO.setTenantId(tenantId);
            clerkPO.setClerkId(userId);
            clerkService.createClerk(clerkPO);
        }
    }

    /**
     * 拒绝加入企业
     * @param userIds
     */
    @Transactional(rollbackFor = Exception.class)
    public void joinDeny(String tenantId,String[] userIds) {
        joinRequestDao.joinReply(tenantId, userIds,-1);
        authUserDao.updateTenantIdBatch(tenantId,userIds);
    }

}
